using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using OpenTK;

namespace MKDS_Course_Editor.NitroIntermediate
{
    class Helper
    {
        public static void FixUV(string file)
        {
            XmlDocument d = new XmlDocument();
            d.Load(file);
            List<int> polymat = new List<int>();
            List<Vector2> texsize = new List<Vector2>();
            foreach (XmlNode n in d.ChildNodes[1].ChildNodes[1].ChildNodes[4].ChildNodes)
            {
                texsize.Add(new Vector2(Convert.ToInt32(n.Attributes[2].Value), Convert.ToInt32(n.Attributes[3].Value)));
            }
            foreach (XmlNode n in d.ChildNodes[1].ChildNodes[1].ChildNodes[9].ChildNodes)
            {
                foreach (XmlNode n2 in n.ChildNodes)
                {
                    polymat.Add(Convert.ToInt32(n2.Attributes[2].Value));
                }
            }
            NSMBe4.ProgressWindow w = new NSMBe4.ProgressWindow("Working");
            w.Show();
            w.SetMax(d.ChildNodes[1].ChildNodes[1].ChildNodes[8].ChildNodes.Count-1);
            w.setValue(0);
            int i = 0;
            foreach (XmlNode n in d.ChildNodes[1].ChildNodes[1].ChildNodes[8].ChildNodes)
            {
                w.setValue(i);
                int id = Convert.ToInt32(n.Attributes[0].Value);
                foreach (XmlNode n2 in n.ChildNodes)
                {
                    foreach (XmlNode n3 in n2.ChildNodes)
                    {
                        foreach (XmlNode n4 in n3.ChildNodes)
                        {
                            foreach (XmlNode n5 in n4.ChildNodes)
                            {
                                if (n5.Name == "tex")
                                {
                                    float s = float.Parse(n5.Attributes[0].InnerText.Split(' ')[0].Replace(".",","));
                                    float t = float.Parse(n5.Attributes[0].InnerText.Split(' ')[1].Replace(".", ","));
                                    while (!(-2048 <= s && s < 2048))
                                    {
                                        if (!(-2048 <= s))
                                        {
                                            try
                                            {
                                            s += texsize[polymat.IndexOf(id)].X;
                                            }
                                            catch
                                            {
                                                s += 8;
                                            }
                                        }
                                        else
                                        {
                                            try
                                            {
                                            s -= texsize[polymat.IndexOf(id)].X;
                                            }
                                            catch
                                            {
                                                s -= 8;
                                            }
                                        }
                                    }
                                    while (!(-2048 <= t && t < 2048))
                                    {
                                        if (!(-2048 <= t))
                                        {
                                            try
                                            {
                                            t += texsize[polymat.IndexOf(id)].Y;
                                            }
                                            catch
                                            {
                                                t += 8;
                                            }
                                        }
                                        else
                                        {
                                            try
                                            {
                                                t -= texsize[polymat.IndexOf(id)].Y;
                                            }
                                            catch
                                            {
                                                t -= 8;
                                            }
                                        }
                                    }
                                    n5.Attributes[0].InnerText = s.ToString("0.000000").Replace(",", ".") + " " + t.ToString("0.000000").Replace(",", ".");
                                }
                            }
                        }
                    }
                }
                i++;
            }
            d.Save(file);
            w.Close();
        }
    }
}
